﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Cassinipp.WebServer.Utilities;

using NUnit.Framework;

namespace Cassinipp.Tests
{
    [TestFixture]
    public class ByteStringTests
    {
        private byte[] FiveZeroBytes { get; set; }
        private byte[] ZeroOneDashedBytes { get; set; }

        private ByteString EmptyByteString { get; set; }
        private ByteString FiveZeroByteString { get; set; }
        private ByteString ZeroOneDashedByteString { get; set; }

        [SetUp]
        public void Setup()
        {
            FiveZeroBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00 };
            ZeroOneDashedBytes = new byte[] { 0x00, 0x01, 0x00, 0x01, 0x00 };

            FiveZeroByteString = new ByteString(FiveZeroBytes, 0, 5);
            EmptyByteString = new ByteString(new byte[0], 0, 0);
            ZeroOneDashedByteString = new ByteString(ZeroOneDashedBytes, 0, 5);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FailBytesNull()
        {
            new ByteString(null, 0, 1);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void FailOffsetAboveByteArrayLength()
        {
            new ByteString(FiveZeroBytes, 5, 1);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void FailOffsetPlusOffsetAboveByteArrayLength()
        {
            new ByteString(FiveZeroBytes, 4, 2);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void FailOffsetLessThanZero()
        {
            new ByteString(FiveZeroBytes, -1, 1);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void FailLenghtIsBelowZero()
        {
            new ByteString(FiveZeroBytes, 1, -1);
        }

        [Test]
        public void TestNotEmptyWithFiveBytes()
        {
            Assert.IsFalse(FiveZeroByteString.IsEmpty);
        }

        [Test]
        public void TestEmpty()
        {
            Assert.IsTrue(EmptyByteString.IsEmpty);
        }

        [Test]
        public void TestSplitEmptyString()
        {
            var splitResult = EmptyByteString.Split('0');

            Assert.IsEmpty(splitResult);
        }

        [Test]
        public void TestSplitResultsAreNotNull()
        {
            var splitResult = ZeroOneDashedByteString.Split('0');

            CollectionAssert.AllItemsAreNotNull(splitResult);
        }
    }
}
